package com.golden.procedure.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.golden.procedure.client.EmployeeClientAPI;
import com.golden.procedure.clientDO.EmployeeDO;
import com.golden.procedure.domain.QueryRequest;
import com.golden.procedure.domain.SingleRequest;
import com.golden.procedure.domain.WorkCommentDO;
import com.golden.procedure.domain.WorkOrderPickDO;
import com.golden.procedure.query.WorkPhotographyQueryDO;
import com.golden.procedure.query.WorkPickQueryDO;
import com.golden.procedure.result.PageResponse;
import com.golden.procedure.result.ResultDO;
import com.golden.procedure.result.SingleResponse;
import com.golden.procedure.service.IWorkOrderPickService;
import com.golden.procedure.service.IWorkerService;
import com.golden.procedure.util.EncryptedUtil;
import com.golden.procedure.util.HttpStatus;
import com.google.gson.Gson;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 选片记录及加选记录等相关接口 <b>Description:</b><br>
 * 
 * @author Reading.Reagan</a>
 * @version 1.0
 * @Note <b>ProjectName:</b> procedure <br>
 *       <b>PackageName:</b> com.golden.procedure.controller <br>
 *       <b>ClassName:</b> WorkOrderPickController <br>
 *       <b>Date:</b> 2018年10月23日 上午10:19:10
 */
@RestController
@RequestMapping(value = "/api/pick")
public class WorkOrderPickController extends BaseController {

	@Autowired
	IWorkOrderPickService workOrderPickService;
	@Autowired
	IWorkerService workerService;
	@Autowired
	EmployeeClientAPI employeeClientAPI;

	public static String encodeObject(SingleResponse<WorkOrderPickDO> response, String encryptType) {
		Gson gson = new Gson();
		String jsonData = gson.toJson(response);
		try {
			return EncryptedUtil.encrypt(jsonData, encryptType);
		} catch (Exception e) {
			e.printStackTrace();
			return SYS_ERROR;
		}
	}

	public static String encodePickObject(SingleResponse<WorkOrderPickDO> response, String encryptType) {
		Gson gson = new Gson();
		String jsonData = gson.toJson(response);
		try {
			return EncryptedUtil.encrypt(jsonData, encryptType);
		} catch (Exception e) {
			e.printStackTrace();
			return SYS_ERROR;
		}
	}

	public static String encodePage(PageResponse<WorkOrderPickDO> response, String encryptType) {
		Gson gson = new Gson();
		String jsonData = gson.toJson(response);
		try {
			return EncryptedUtil.encrypt(jsonData, encryptType);
		} catch (Exception e) {
			e.printStackTrace();
			return SYS_ERROR;
		}
	}

	public static String encodeOrderPickPage(PageResponse<WorkOrderPickDO> response, String encryptType) {
		Gson gson = new Gson();
		String jsonData = gson.toJson(response);
		try {
			return EncryptedUtil.encrypt(jsonData, encryptType);
		} catch (Exception e) {
			e.printStackTrace();
			return SYS_ERROR;
		}
	}

	@ApiOperation(value = "查询看样选片记录列表", notes = "查询看样选片记录列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "schedulingDateStr", value = "日期（时间戳）", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "option", value = "0-员工；1-部门；2-全部", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "queryKey", value = "其他查询条件", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "nameOrTel", value = "查询条件:客户姓名/电话、摄影组人员姓名", required = false, dataType = "String") })
	@RequestMapping(value = "/query.json", method = RequestMethod.POST)
	public String query(@RequestBody QueryRequest<WorkPhotographyQueryDO> request) {

		// {"data":
		// {"schedulingDateStr":"1535990400000","orgID":1},"version":1,"token":"W57m+iUG3/D+y/KY6V8ktuboDcz0nFIw62hRSUwHmUA="}
		PageResponse<WorkOrderPickDO> response = new PageResponse<WorkOrderPickDO>();

		WorkPhotographyQueryDO query = request.getData();
		if (query.getPageRows() > 0) {
			query.setPage(true);
		}
		// TODO: 0-员工；1-部门；2-全部
		if (query.getOption() == 0) {
			query.setEmployeeID(12);
		} else if (query.getOption() == 1) {
			// TODO: 获取当前登录人员所在部门id
			// EmployeeDO employeeDO = employeeClientAPI.get(11);
			// if (null == employeeDO) {
			// // 员工查询失败
			// response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			// response.setVersion(1);
			// response.setMsg("员工信息获取失败");
			// return encodePage(response, PLANINTEXT);
			// }
			// query.setOrgID(employeeDO.getOrgId());
			query.setOrgID(1);
		}

		ResultDO resultDO = workOrderPickService.query(query);
		if (resultDO.isSuccess()) {
			response.setStatus(HttpStatus.SUCCESS.getErrorCode());
			response.setVersion(1);
			response.setMsg(HttpStatus.SUCCESS.getMsg());
			response.setData((List<WorkOrderPickDO>) resultDO.getModel(ResultDO.FIRST_MODEL_KEY));
			response.setCount((int) resultDO.getModel(ResultDO.THIRD_MODEL_KEY));
			if (query.isPage()) {
				response.setTotal((int) resultDO.getModel(ResultDO.SECOND_MODEL_KEY));
			}
		} else {
			// 查询失败
			response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			response.setVersion(1);
			response.setMsg(resultDO.getErrorMsg());
		}

		return encodePage(response, PLANINTEXT);
	}

	@ApiOperation(value = "添加选片完成记录", notes = "添加选片完成记录")
	@ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "选片记录id", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "status", value = "1-进行中；2-重拍; 3-补拍; 4-改期; 5-完成; -1取消", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "remark", value = "备注", required = true, dataType = "Integer") })
	@RequestMapping(value = "/record.json", method = RequestMethod.POST)
	public String record(@RequestBody SingleRequest<WorkCommentDO> request) {

		WorkCommentDO commissionDO = request.getData();

		commissionDO.setAuthorID(9);
		commissionDO.setAuthor("test");

		SingleResponse<WorkOrderPickDO> response = new SingleResponse<WorkOrderPickDO>();
		ResultDO resultDO = workOrderPickService.record(commissionDO);
		if (resultDO.isSuccess()) {
			response.setStatus(HttpStatus.SUCCESS.getErrorCode());
			response.setVersion(0);
			response.setMsg(HttpStatus.SUCCESS.getMsg());
		} else {
			response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			response.setVersion(1);
			response.setMsg(resultDO.getErrorMsg());
		}
		return encodeObject(response, PLANINTEXT);
	}

	@ApiOperation(value = "查询加选订单", notes = "查询加选订单")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "option", value = "0-本人；1-本部门；2-全部", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "status", value = "状态：0-未提交;1-已提交;2-已支付;", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "nameOrTel", value = "姓名/手机号", required = false, dataType = "String") })
	@RequestMapping(value = "/queryPickOrder.json", method = RequestMethod.POST)
	public String queryPickOrder(@RequestBody QueryRequest<WorkPickQueryDO> request) {

		PageResponse<WorkOrderPickDO> response = new PageResponse<WorkOrderPickDO>();

		WorkPickQueryDO queryDO = request.getData();
		if (queryDO.getPageRows() > 0) {
			queryDO.setPage(true);
		}
		// TODO: 0-员工；1-部门；2-全部
		if (queryDO.getOption() == 0) {
			queryDO.setEmployeeID(11);
		} else if (queryDO.getOption() == 1) {
			queryDO.setOrgID(1);
			// TODO: 获取当前登录人员所在部门id
			EmployeeDO employeeDO = employeeClientAPI.get(11);
			if (null == employeeDO) {
				// 员工查询失败
				response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
				response.setVersion(1);
				response.setMsg("员工信息获取失败");
				return encodePage(response, PLANINTEXT);
			}
			queryDO.setOrgID(employeeDO.getOrgId());
		}

		ResultDO resultDO = workOrderPickService.queryByBusinessNo(queryDO);
		if (resultDO.isSuccess()) {
			response.setStatus(HttpStatus.SUCCESS.getErrorCode());
			response.setVersion(0);
			response.setMsg(HttpStatus.SUCCESS.getMsg());
			response.setData((List<WorkOrderPickDO>) resultDO.getModel(ResultDO.FIRST_MODEL_KEY));
			if (queryDO.isPage()) {
				response.setTotal((int) resultDO.getModel(ResultDO.SECOND_MODEL_KEY));
			}
		} else {
			response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			response.setVersion(1);
			response.setMsg(resultDO.getErrorMsg());
		}
		return encodeOrderPickPage(response, PLANINTEXT);
	}

	@ApiOperation(value = "取消提交的加选订单", notes = "取消提交的加选订单")
	@ApiImplicitParams({ @ApiImplicitParam(name = "businessNo", value = "销售业务单号", required = true, dataType = "String"),
			@ApiImplicitParam(name = "workOrderID", value = "工单ID", required = true, dataType = "Integer") })
	@RequestMapping(value = "/cancelPickOrder.json", method = RequestMethod.POST)
	public String cancelPickOrder(@RequestBody SingleRequest<WorkOrderPickDO> request) {

		WorkOrderPickDO domain = request.getData();

		SingleResponse<WorkOrderPickDO> response = new SingleResponse<WorkOrderPickDO>();
		ResultDO resultDO = workOrderPickService.cancelPickOrder(domain);
		if (resultDO.isSuccess()) {
			response.setStatus(HttpStatus.SUCCESS.getErrorCode());
			response.setVersion(0);
			response.setMsg(HttpStatus.SUCCESS.getMsg());
		} else {
			response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			response.setVersion(1);
			response.setMsg(resultDO.getErrorMsg());
		}
		return encodePickObject(response, PLANINTEXT);
	}

	@ApiOperation(value = "删除加选订单", notes = "删除加选订单")
	@ApiImplicitParams({ @ApiImplicitParam(name = "businessNo", value = "销售业务单号", required = true, dataType = "String"),
			@ApiImplicitParam(name = "workOrderID", value = "工单ID", required = true, dataType = "Integer") })
	@RequestMapping(value = "/delPickOrder.json", method = RequestMethod.POST)
	public String delPickOrder(@RequestBody SingleRequest<WorkOrderPickDO> request) {

		WorkOrderPickDO domain = request.getData();
		domain.setModifiID(9);
		domain.setModifi("test");

		SingleResponse<WorkOrderPickDO> response = new SingleResponse<WorkOrderPickDO>();
		ResultDO resultDO = workOrderPickService.delPickOrder(domain);
		if (resultDO.isSuccess()) {
			response.setStatus(HttpStatus.SUCCESS.getErrorCode());
			response.setVersion(0);
			response.setMsg(HttpStatus.SUCCESS.getMsg());
		} else {
			response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			response.setVersion(1);
			response.setMsg(resultDO.getErrorMsg());
		}
		return encodePickObject(response, PLANINTEXT);
	}

	@ApiOperation(value = "test更新", notes = "")
	@ApiImplicitParams({ @ApiImplicitParam(name = "businessNo", value = "销售业务单号", required = true, dataType = "String"),
			@ApiImplicitParam(name = "workOrderID", value = "工单ID", required = true, dataType = "Integer") })
	@RequestMapping(value = "/test.json", method = RequestMethod.POST)
	public String test(@RequestBody SingleRequest<WorkOrderPickDO> request) {

		WorkOrderPickDO domain = request.getData();

		SingleResponse<WorkOrderPickDO> response = new SingleResponse<WorkOrderPickDO>();
		ResultDO resultDO = workOrderPickService.updateAmount(domain);
		if (resultDO.isSuccess()) {
			response.setStatus(HttpStatus.SUCCESS.getErrorCode());
			response.setVersion(0);
			response.setMsg(HttpStatus.SUCCESS.getMsg());
		} else {
			response.setStatus(HttpStatus.SYSTEM_ERROR.getErrorCode());
			response.setVersion(1);
			response.setMsg(resultDO.getErrorMsg());
		}
		return encodePickObject(response, PLANINTEXT);
	}

}
